<template>
  <div class="manage">
    <el-dialog
        :title="operateType === 'add' ? '新增用户' : '更新用户'"
        :visible.sync="isShow"
    >
      <common-form
        :formAttr="userFormAttr"
        :formData="userFormData"
        :inline="true"
        ref="userForm"
      ></common-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow=false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>

    <div class="manage-header">
      <el-button type="primary" @click="addUser">+ 新增</el-button>
      <common-form style="padding-top: 12px"
          :formAttr="searchFormAttr"
          :formData="searchFormData"
          :inline="true"
          ref="searchForm"
      >
        <el-button type="primary" @click="handleSearch(searchFormData.keyword)">搜索</el-button>
      </common-form>
    </div>

    <div class="manage-table">
      <common-table
          :tableAttr="userTableAttr"
          :tableData="userTableData"
          :pageConfig="userPageConfig"
          @changePage="changePage"
          @sizeChange="sizeChange"
          @edit="editUser"
          @del="delUser"
      ></common-table>
    </div>
  </div>
</template>

<script>
import CommonForm from "@c/CommonForm";
import CommonTable from "@c/CommonTable";
import { addUser, editUser, deleteUser, getUserList } from "../../api/data";

export default {
  components: {
    CommonForm, CommonTable
  },
  data() {
    return {
      operateType: 'add',
      isShow: false,
      userFormAttr: [
        {
          name: 'name',
          label: '姓名',
          type: 'input'
        },
        {
          name: 'age',
          label: '年龄',
          type: 'input'
        },
        {
          name: 'sex',
          label: '性别',
          type: 'select',
          opts: [
            {
              label: '男',
              value: 1
            },
            {
              label: '女',
              value: 0
            }
          ]
        },
        {
          name: 'birth',
          label: '出生日期',
          type: 'date'
        },
        {
          name: 'addr',
          label: '地址',
          type: 'input'
        }
      ],
      userFormData: {
        id: '',
        name: '',
        addr:'',
        age:'',
        birth:'',
        sex:''
      },
      searchFormAttr: [
        {
          name: 'keyword',
          label: '',
          type: 'input'
        }
      ],
      searchFormData: {
        keyword: ''
      },
      userTableAttr: [
        {
          field: 'id',
          label: '序号',
          width: 80
        },
        {
          field: 'name',
          label: '姓名',
          width: 80
        },
        {
          field: 'age',
          label: '年龄',
          width: 80
        },
        {
          field: 'sexLabel',
          label: '性别',
          width: 80
        },
        {
          field: 'birth',
          label: '出生日期',
          width: 140
        },
        {
          field: 'addr',
          label: '地址'
        }
      ],
      userTableData: [],
      userPageConfig: {
        total: 30,
        currentPage: 1,
        pageSizes: [10, 20, 30, 40],
        pageSize: 10,
        loading: true
      },
      params: {}
    }
  },
  created() {
    this.getList(this.params)
  },
  methods: {
    confirm() {
      // console.log(this.userFormData);
      if (this.operateType == 'edit') {
        // 更新
        editUser(this.userFormData)
          .then(res => {
            console.log(res);
            this.isShow =false
            this.getList()
          })
      } else {
        // 新增
        addUser(this.userFormData)
          .then(res => {
            console.log(res);
            this.isShow = false
            this.getList()
          })
      }
    },
    addUser() {
      this.isShow = true;
      this.operateType = 'add'
      this.userFormData = {
        name: '',
        addr:'',
        age:'',
        birth:'',
        sex:''
      }
    },
    handleSearch() {
      this.params.name = this.searchFormData.keyword;
      this.getList();
    },
    getList() {
      this.userPageConfig.loading = true
      if (this.params.name !== undefined) {
        this.userPageConfig.page = 1
      }
      getUserList(this.params)
        .then(res=> {
          const { list, total } = res.data
          this.userTableData = list.map(item => {
            item.sexLabel = item.sex === 0 ? '男' : '女'
            return item
          })
          console.log('getUserList.total:' + total)
          this.userPageConfig.total = total
          this.userPageConfig.loading = false
      })
    },
    editUser(row) {
      this.operateType = 'edit'
      Object.assign(this.userFormData, row)
      this.isShow = true
    },
    delUser(row) {
      this.$confirm("此操作将永久删除该数据，是否继续？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteUser({id: row.id})
          .then(res => {
            console.log(res)
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.getList()
          })
      })
    },
    changePage(page) {
      this.params.page = page
      this.getList()
    },
    sizeChange(size) {
      console.log(size)
      this.params.limit = size
      this.params.page = 1
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
  .manage {
    height: 100%;
    .manage-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .manage-table {
      height: calc(100% - 62px);
    }
  }
</style>